<?php

namespace Home\Controller;


/**
 * 订单控制器
 *
 */
class OrderController extends HomeController
{

    /**
     * 订单列表
     */
    public function index()
    {
        $uid = $this->uid;
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        if (empty($one['name'])) {
            $this->showError('请先完善信息');
        }
        $model = D('Dingdan');
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));
        $status = I('get.status', -1);

        $type = I('get.type');
        $danhao = I('get.danhao');
        $export = I('get.export', false);
        $cat = I('get.cat'); //订单分类：1=支付宝，2=银行卡
        $where = array();

        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {

            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {

            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {

            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $where['time'] = $time;
        }
        if (!empty($danhao)) {
            if (strlen($danhao) == 28) {
                $where['trade_no'] = $danhao;
            } else {
                $where['danhao'] = $danhao;
            }

        }
        if ($status != -1) {

            $where['status'] = $status;
        }
        if ($type != -1) {

            $where['type'] = $type;
        }

        $where['uid'] = $this->uid;
        // 数据整合开始
        $a = D('Dingdan')->where(['uid' => $uid])->select();
        $b = D('Dividend')->where(['uid' => $uid])->select();

        $lists = array();
        $key = 0;

        //收益
        foreach ($a as $v) {//订单收益
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['pay_money'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 1;
            $lists[$key]['time'] = $v['pay_time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }


        $data = [];
        foreach ($lists as $k => $v) {

            if ($cat == 1) {
                if (strpos($v['danhao'], 'hb') !== false) {
                    //支付宝
                    array_push($data, $v);
                }
            } else {
                if (strpos($v['danhao'], 'xy') !== false) {
                    //信用卡
                    array_push($data, $v);
                }
            }

        }

        $this->showSuccess('', $data);

    }

    /**
     * 我的佣金列表
     */
    public function myyongjin()
    {
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));

        $where = array();
        $where['uid'] = $this->uid;
        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {

            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {

            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {

            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $where['time'] = $time;
        }

        //口碑
        $list = M('split')->where($where)->field('id,sh_money,status,time,out_request_no')->select();

        $total3 = D('Split')->where(['uid' => $this->uid, 'status' => 1])->sum('sh_money'); //口碑收益获得
        is_null($total3) ? $yongjin = 0.00 : $yongjin = $total3; //口碑收益是佣金
        $this->showSuccess('', ['account' => $yongjin, 'list' => $list]);

    }

    /**
     * 我的分润列表
     */
    public function myfenrun()
    {

        $uid = $this->uid;
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));
        $type = I('get.type', 0);

        $where = array();
        $where['uid'] = $this->uid;

        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {

            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {

            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {

            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $where['time'] = $time;
        }

        if ($type != 0) {
            $where['type'] = $type;
        }
        $list = D('Dividend')->where($where)->field('id,divide,time,status,danhao,level,type')->select();
        $fenrun = D('Dividend')->where($where)->sum('divide'); //推广获得
        $this->showSuccess('', ['fenrun' => $fenrun, 'list' => $list]);
    }


    /**
     * 花呗支付 同步跳转方法
     * 调用分账方法
     */
    public function huabeiReturnUrl()
    {
        $url = "http://qnm.upslope.cn/admin/test/test";
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
    }


}